<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 4.3.2 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Custom CodeIgniter FCK Helpers
 *
 * @package		Exa
 * @subpackage	Helpers
 * @category	Helpers
 * @author		Exa
 * @link		NA
 */

// ------------------------------------------------------------------------		

/**
  * Form Fck Editor
  *
  * @access public
  * @param string
  * @param string
  * @param string
  * @return string/iframe
  * 
  * Example:
	****** USAGE ******

	// e.g. 1
	echo form_fckeditor('textareaName', $value );
	
	
	// e.g. 2
	$data = array(
				  'name'        => 'textareaName2',
				  'id'          => 'textareaName2',
				  'toolbarset'  => 'Advanced',
				  'basepath'    => '/fckeditor/',
				  'width'       => '100%',
				  'height'      => '200'
		);
	
	echo form_fckeditor( $data ); 

  */
include_once( BASEPATH . '/helpers/form_helper'.EXT);

function form_fckeditor($data = '', $value = '', $extra = '')
{
    $CI =& get_instance();

    $fckeditor_basepath = $CI->config->item('fckeditor_basepath');
    
    require_once( ROOTBASEPATH . $fckeditor_basepath. 'fckeditor.php' );
    
    $instanceName = ( is_array($data) && isset($data['name'])  ) ? $data['name'] : $data;
    $fckeditor = new FCKeditor($instanceName);
    
    if( $fckeditor->IsCompatible() )
    {
        $fckeditor->Value = html_entity_decode($value);
        $fckeditor->BasePath = base_url() . $fckeditor_basepath;
        if( $fckeditor_toolbarset = $CI->config->item('fckeditor_toolbarset_default'))
                $fckeditor->ToolbarSet = $fckeditor_toolbarset;
        
        if( is_array($data) )
        {
            if( isset($data['value']) )
                $fckeditor->Value = html_entity_decode($data['value']);
            if( isset($data['basepath']) )
                $fckeditor->BasePath = $data['basepath'];
            if( isset($data['toolbarset']) )
                $fckeditor->ToolbarSet = $data['toolbarset'];
            if( isset($data['width']) )
                $fckeditor->Width = $data['width'];
            if( isset($data['height']) )
                $fckeditor->Height = $data['height'];
        }
        
        
        return $fckeditor->CreateHtml();
    }
    else
    {
        return form_textarea( $data, $value, $extra );
    }
}

// ------------------------------------------------------------------------	
	
/**
 * Get Page Title
 *
 * @access 	public
 * @param 	string
 * @return 	string
 *
 */
if(!function_exists('get_page_title')) 
{
	function get_page_title($attach='')
	{
		$str = OS_CRM_TITLE;
		
		if(!empty($attach))
			$str .= ' :: '.$attach;
		
		return $str;
	}
}
// --------------------------------------------------------------------------

/**
 * Remove Illegal Characters
 *
 * @access public
 * @param  string
 * @return string
 */
if(!function_exists('remove_illegal_chars')) 
{ 
	function remove_illegal_chars($input) 
	{	 
		$illegal_chars = array(
							"!", "#", "$", "%", "^", 
							"&", "*", "(", ")", "+", 
							"=", "|", "\\", "{", "[", 
							"}", "]", ":", ";", "\"", 
							"'", "<", ",", ">", "?", 
							"/", "~", "`", " "
						  );
		
		for ($i = 0; $i < count($illegal_chars); $i++) 
		{
			$input = str_replace($illegal_chars[$i], "", $input);
			$input = str_replace(" ", "_", $input);
			$input = str_replace("-", "_", $input);
		}
		
		return($input);
		
	}
}

// --------------------------------------------------------------------------

/**
 * Get User Details
 *
 * @access public
 * @param  string
 * @return string
 */
if(!function_exists('get_user_details')) 
{ 
	function get_user_details($search='') 
	{
		$CI =& get_instance();
		$return_data = "";
		
		$query = "SELECT *
					FROM 
						".TBL_USER." 
					WHERE 
						user_id = '".($CI->session->userdata('user_id'))."' 
					";
					
		$query = $CI->db->query($query);

		if ($query->num_rows > 0)
		{
			$result_set = $query->row_array();
						
			if(is_array($search) && count($search) > 0)			
			{
				foreach($search as $field)
				{
					$return_data[$field] = (isset($result_set[$field])) ? $result_set[$field]: '';	
				}
			}
			else
			{
				$return_data = (isset($result_set[$search])) ? $result_set[$search]: '';
			}
		}
		
		return $return_data;	
	}
}

// --------------------------------------------------------------------------

/**
 * Display Number Format
 *
 * @access public
 * @param  integer
 * @return mixed
 */
if(!function_exists('display_number_format')) 
{ 
	function display_number_format($no=0) 
	{
		$formated = 0;
		
		if(!empty($no))
			$formated = number_format($no,2,'.',',');
		
		return $formated;				
	}
}


// ------------------------------------------------------------------------

/**
 * Log all the unique information of the data 
 *
 * @access	public
 * @param	string
 * @param	array
 * @return	void
 */	
if ( ! function_exists('log_cron_updates'))
{
	function log_cron_updates ($desc, $updates)
	{
		$CI =& get_instance();
		$data = array(
					'controller'	=> $CI->session->CI->router->class,
					'description'	=> $desc,
					'updates' 		=> serialize($updates)
				);	
		
		$CI->db->insert('cron_update_log', $data);		
	}
}

	
// --------------------------------------------------------------------

/**
 * Maintain Log
 *
 * @author Dhaval K 21/Sept/2009
 *
 * This function maintains the elapsed_time & memory_usage of the script
 * This is added to render_page_log for further investigations
 *
 * @access	public
 * @return	string
 */
function render_page_log($url = '', $user = '', $elapsed_time = 0, $memory_usage = 0)
{
	$CI =& get_instance();
	$data['url'] = $url;
	$data['user'] = $user;
	$data['total_time'] = $elapsed_time;
	$data['memory_usage'] = $memory_usage;

	$CI->db->insert(TBL_REN_PAGE_LOG, $data);
}

// ------------------------------------------------------------------------	
	
/**
 * Set Meta Tags
 *
 * @access 	public
 * @return 	string/void
 */
function set_meta_tags()
{
	$str = '';
	$str .= '<meta name="robots" content="noindex,nofollow" />';
	$str .= '<meta name="robots" content="noarchive" />';
	
	if(defined('SET_NO_INDEX_TAGS') && SET_NO_INDEX_TAGS == true)
		echo $str;
}	

// --------------------------------------------------------------------------

/**
 * Query date validation
 *
 * @access public
 * @param  string
 * @param  string
 * @param  string
 * @return mixed 
 * query skeleton:
 * ( 
 *  site_afs_date != 'NULL' AND 
 * 	site_afs_date != '0000-00-00 00:00:00' AND 
 *	site_afs_date != '' AND 
 *	site_afs_date IS NOT NULL AND 
 *	SUBSTR(site_afs_date, 1, 4) != '1970' AND 
 *	SUBSTR(site_afs_date, 1, 4) != '1900' 
 * )
 *
 */
if(!function_exists('query_date_val')) 
{ 
	function query_date_val($field = '',$comparison = '=',$logical = 'AND') 
	{
		$qry 	= '';		
		if(!empty($field))
		{
			$qry .= " ( ";			
			$qry .= "  		$field $comparison 'NULL' ";
			$qry .= " 	$logical ";
			$qry .= " 		$field $comparison '0000-00-00 00:00:00' "; 
			$qry .= " 	$logical ";
			$qry .= " 		$field $comparison '' ";
			$qry .= " 	$logical ";
			if($comparison == '=')
				$qry .= " 		$field IS NULL ";	
			else
				$qry .= " 		$field IS NOT NULL ";	
			$qry .= " 	$logical ";
			$qry .= " 		SUBSTR($field, 1, 4) $comparison '1970' ";
			$qry .= " 	$logical ";
			$qry .= " 		SUBSTR($field, 1, 4) $comparison '1900' ";
			$qry .= " ) ";						
		}	
		
		return $qry;				
	}
}
//EOF Form Helper
?>